﻿/* MDA.Net - .Net port of mda-vst plugins
 * 
 * .Net port, Copyright (C) 2011, Balint Pfliegel
 * Based on mda-vst, Copyright (C) 1999-2001 Paul Kellett (maxim digital audio)
 * Based on VST2 SDK (c) 1996-1999 Steinberg Soft und Hardware GmbH, All Rights Reserved
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be
 * useful, but WITHOUT ANY WARRANTY; without even the implied
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
 * PURPOSE.  See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301 USA.
 */

using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO;
using System.Reflection;

namespace MDA.Net.ResourceManagers
{
    public class MDAKGRP  //keygroup
    {
        public Int32 root;  //MIDI root note
        public Int32 high;  //highest note
        public Int32 pos;
        public Int32 end;
        public Int32 loop;
    };

    public class MDAPianoData
    {
        #region Singleton

        private static object staticLock = new object();
        private static MDAPianoData _instance = null;
        public static MDAPianoData Instance
        {
            get
            {
                lock (staticLock)
                {
                    if (_instance == null)
                    {
                        _instance = new MDAPianoData();
                    }
                }
                return _instance;
            }
        }

        private Stream GetBinaryResourceStream(string resourceName)
        {
            Assembly assembly = Assembly.GetExecutingAssembly();
            BinaryReader reader = new BinaryReader(assembly.GetManifestResourceStream(resourceName));
            return reader.BaseStream;
        }

        private MDAPianoData()
        {
            // Read samples
            Stream s = GetBinaryResourceStream("MDA.Net.Resources.MDAPiano.MDAPianoData.raw");
            BinaryReader br = new BinaryReader(s);
            int length = (int)br.BaseStream.Length / 2;
            Data = new short[length];

            for (int i = 0; i < length; i++)
            {
                Data[i] = br.ReadInt16();
            }

            br.Dispose();
            s.Close();
            s.Dispose();

            // Initialize keygroups
            //Waveform data and keymapping is hard-wired in *this* version
            for (int i = 0; i < 15; i++)
            {
                Keygroup[i] = new MDAKGRP();
            }
            Keygroup[0].root = 36; Keygroup[0].high = 37; Keygroup[0].pos = 0; Keygroup[0].end = 36275; Keygroup[0].loop = 14774;
            Keygroup[1].root = 40; Keygroup[1].high = 41; Keygroup[1].pos = 36278; Keygroup[1].end = 83135; Keygroup[1].loop = 16268;
            Keygroup[2].root = 43; Keygroup[2].high = 45; Keygroup[2].pos = 83137; Keygroup[2].end = 146756; Keygroup[2].loop = 33541;
            Keygroup[3].root = 48; Keygroup[3].high = 49; Keygroup[3].pos = 146758; Keygroup[3].end = 204997; Keygroup[3].loop = 21156;
            Keygroup[4].root = 52; Keygroup[4].high = 53; Keygroup[4].pos = 204999; Keygroup[4].end = 244908; Keygroup[4].loop = 17191;
            Keygroup[5].root = 55; Keygroup[5].high = 57; Keygroup[5].pos = 244910; Keygroup[5].end = 290978; Keygroup[5].loop = 23286;
            Keygroup[6].root = 60; Keygroup[6].high = 61; Keygroup[6].pos = 290980; Keygroup[6].end = 342948; Keygroup[6].loop = 18002;
            Keygroup[7].root = 64; Keygroup[7].high = 65; Keygroup[7].pos = 342950; Keygroup[7].end = 391750; Keygroup[7].loop = 19746;
            Keygroup[8].root = 67; Keygroup[8].high = 69; Keygroup[8].pos = 391752; Keygroup[8].end = 436915; Keygroup[8].loop = 22253;
            Keygroup[9].root = 72; Keygroup[9].high = 73; Keygroup[9].pos = 436917; Keygroup[9].end = 468807; Keygroup[9].loop = 8852;
            Keygroup[10].root = 76; Keygroup[10].high = 77; Keygroup[10].pos = 468809; Keygroup[10].end = 492772; Keygroup[10].loop = 9693;
            Keygroup[11].root = 79; Keygroup[11].high = 81; Keygroup[11].pos = 492774; Keygroup[11].end = 532293; Keygroup[11].loop = 10596;
            Keygroup[12].root = 84; Keygroup[12].high = 85; Keygroup[12].pos = 532295; Keygroup[12].end = 560192; Keygroup[12].loop = 6011;
            Keygroup[13].root = 88; Keygroup[13].high = 89; Keygroup[13].pos = 560194; Keygroup[13].end = 574121; Keygroup[13].loop = 3414;
            Keygroup[14].root = 93; Keygroup[14].high = 999; Keygroup[14].pos = 574123; Keygroup[14].end = 586343; Keygroup[14].loop = 2399;
        }

        #endregion

        #region Data

        public short[] Data = null;
        public MDAKGRP[] Keygroup = new MDAKGRP[15];

        #endregion
    }
}
